package T01_JDBC的核心API;

import java.sql.*;

/**
 * 中间多度讲解 书写多个next()
 */
public class T07_SelectResultSet02 {
    public static void main(String[] args) {
        Connection connection=null;
        Statement stmt=null;
        ResultSet rs=null;
        try{
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3309/test?useSSL=false","root","root");
            //3.定义sql
            String sql="select * from user";
            //4获取执行sql对象
            stmt=connection.createStatement();
            //5.执行sql
            rs=stmt.executeQuery(sql);
            //6.处理结果
            //6.1 让游标向下移动一行
            rs.next();
            //6.2 获取数据1
            int id = rs.getInt(1);
            String name = rs.getString("name");
            String password=rs.getString(3);
            System.out.println(id+"-"+name+"-"+password);
            rs.next();
            //6.2 获取数据2
            int id2 = rs.getInt(1);
            String name2 = rs.getString("name");
            String password2=rs.getString(3);
            System.out.println(id2+"-"+name2+"-"+password2);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
